/*
	@author:Lu Zhaocheng
	@createTime:2020/09/30
	@finishTime:2020/10/05
*/
#include <bits/stdc++.h>

#define Status int
#define OK 1
#define Fail 0
using namespace std;
typedef int ElemType;
typedef struct LNode {
    ElemType data;
    struct LNode *next;
} LNode, *LinkList;
int LinkLength = 0;

LinkList initLink(int s[]) {
    LinkList L = new LNode;
    LinkList tmpL = L;
    tmpL->next = NULL;
    for (int i = 0; i < sizeof(s); i++) {
        LinkList p = new LNode;
        p->data = s[i];
        tmpL->next = p;
        tmpL = p;
        LinkLength++;
    }
    tmpL->next = NULL;
    return L;
}

Status GetElem(LinkList p, int i, ElemType &e) {
    int count = 0;
    LinkList tmpL = p;
    while (tmpL->next && count < i) {
        tmpL = tmpL->next;
        count++;
    }
    e = tmpL->data;
    return count < i ? OK : Fail;
}

Status SearchElem(LinkList p, ElemType e, int &loc) {
    int count = 0;
    LinkList tmpL = p;
    Status flag = Fail;
    while (tmpL->next && tmpL->data != e) {
        count++;
        tmpL = tmpL->next;
    }
    if (tmpL->data == e) {
        flag = OK;
        loc = count;
    }
    return flag;
}

Status InsElem(LinkList p, int loc, ElemType e) {
    if (loc < 1 || loc > LinkLength) return false;
    int count = 1;
    LinkList tmpL = p;
    while (tmpL->next && count < loc) {
        count++;
        tmpL = tmpL->next;
    }
    LinkList s = new LNode;
    s->data = e;
    s->next = tmpL->next;
    tmpL->next = s;
    LinkLength++;
}

void print_link(string s, LinkList p) {
    LNode* tmpL = p;
    cout << s << ": ";
    while (tmpL->next) {
        tmpL = tmpL->next;
        cout << tmpL->data << " ";
    }
    cout << endl;
}

Status DelElem(LinkList p, int loc) {
    if (loc < 0 || loc > LinkLength) return false;
    int count = 1;
    LinkList tmpL = p;
    while (count < loc) {
        tmpL = tmpL->next;
        count++;
    }
    LinkList s = new LNode;
    s->next = tmpL->next->next;
    tmpL->next = s->next;
    LinkLength--;
    return true;
}

Status InsHeadElem(LinkList p, ElemType e) {
    LinkList tmpL = p;
    LinkList s = new LNode;
    s->next = tmpL->next;
    s->data = e;
    tmpL->next = s;
}

Status InsTailElem(LinkList p, ElemType e) {
    LinkList tmpL = p;
    LinkList s = new LNode;
    while (tmpL->next) tmpL = tmpL->next;
    s->data = e;
    s->next = NULL;
    tmpL->next = s;
}

Status ClearLinkList(LinkList p) {
    LinkList tmpL = p;
    LinkList s = tmpL->next;
    while (tmpL->next) {
        tmpL->next = s->next;
        s->next = NULL;
        free(s);
        s = tmpL->next;
    }
    free(p);
    return OK;
}

LinkList UnionLinkList(LinkList p, LinkList q) {
    LinkList L = new LNode;
    LinkList tmpL = L;
    LinkList tmpP = p;
    LinkList tmpQ = q;
    while (tmpP->next) {
        LinkList s = new LNode;
        tmpP = tmpP->next;
        s->data = tmpP->data;
        s->next = NULL;
        tmpL->next = s;
        tmpL = s;
    }
    while (tmpQ->next) {
        LinkList s = new LNode;
        tmpQ = tmpQ->next;
        s->data = tmpQ->data;
        s->next = NULL;
        tmpL->next = s;
        tmpL = s;
    }
    return L;
}

LinkList CopyList(LinkList p) {
    LinkList L = new LNode;
    LinkList tmpL = L;
    LinkList tmpP = p;
    while (tmpP->next) {
        LinkList s = new LNode;
        tmpP = tmpP->next;
        s->data = tmpP->data;
        s->next = NULL;
        tmpL->next = s;
        tmpL = s;
    }
    return L;
}
